<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* clientHandler Class
*
* This class holds everything serializes new data to the client
* like other handlers, it is not instanced
* 
*/

class clientHandler {
	
	// This can be passed from literally ANYWHERE so be careful!
	public static function change($object,$prop_name,$property) {
		$objecttype = get_class($object);
		
		// This is NOT how this is supposed to be handled
		// But it is suitable to retain existing functionality
		// Until a total architecture can be set down
		if($objecttype=="UserCharacter") {
			$object->$prop_name = $property; // Make the change
			
			if (is_string($property)) { $object->send($property,$prop_name); }
			else { clientHandler::$prop_name($object); } // Write it to the client
		}
	}
	
	// This can be passed from literally ANYWHERE so be careful!
	public static function toggleFlag($object,$flag) {
		$flag_is_on = $object->has($flag); // Did they turn it on or off?
		$objecttype = get_class($object);
		// This will just end up as a massive switch statement where each flag
		// tosses off its' own behavior by object and by flag status
	}
	
	public static function load($user) {
		$user->send_data(clientHandler::serializeobject($user)); // Eventually we will serialize more specifically...
		$user->send_data(clientHandler::serialize_in_room($user)); 
		$user->send_data(clientHandler::serialize_whois_data($user));
		$user->send_data(array("loaded"=>true)); // To show we loaded
	}
	
	public static function in_object($user) {
		$user->send_data(clientHandler::serialize_in_room($user));
	}
	
	public static function was_in_object($user) {
		// Right now, do nothing
	}
	
	public static function whois($user) {
		$user->send_data(clientHandler::serialize_whois_data($user));
	}
	
	public static function updatePeopleInside($user) {
		$user->send_data(clientHandler::serialize_people($user));
	}
	
	public static function roomList($user) {
		$user->send_data(clientHandler::serialize_room_list($user));
	}
	
	/* ----------------- Serializers */
	
	// objects consist of simple stuff, so it's easy to serialize them directly
	public static function serializeobject($object) {
		$tmp = array();
		if (isset($object->name)) 			{ $tmp["name"] = $object->name; 				}
		if (isset($object->short_descr)) 	{ $tmp["short_descr"] = $object->short_descr;	}
		if (isset($object->description)) 	{ $tmp["description"] = $object->description;	}
		if (isset($object->thumb)) 			{ $tmp["thumb"] = $object->thumb;				}
		if (isset($object->image)) 			{ $tmp["image"] = $object->image;				}
		return $tmp;
	}

	public static function serialize_whois_data($user) {
		$tmp = array();
		// Loop through the people in the world
		$i = 0;
		// This will eventually have to call out to see how much information
		// the user is allowed to know about the given person
		// Until then, we simply take everyone in the world and give their critical stats
		foreach(World::$char_list as $person) {
			$tmp += array($i=>clientHandler::serialize_whois_person($person));
			$i++;
		}
		
		return array("whois"=>$tmp);
	}
	
	public static function serialize_whois_person($user) {
		$tmp = array();
		$tmp += array("name"=>$user->name);
		$tmp += array("image"=>$user->image);
		return $tmp;
	}
	
	public static function serialize_visible_person($user) {
		$tmp = array();
		$tmp += clientHandler::serializeobject($user);
		return $tmp;
	}
	
	public static function serialize_visible_exit($exit) {
		$tmp = array();
		$tmp += clientHandler::serializeobject($exit);
		$tmp += array("command"=>$exit->command);
		return $tmp;
	}
	
	public static function serialize_visible_item($item) {
		$tmp = array();
		$tmp += clientHandler::serializeobject($item);
		return $tmp;
	}
	
	public static function serialize_visible_exits($user) {
		$room=$user->in_room();
		$tmp = array();
		
		// The Exits in the room
		if (isset($room->exits)) {
			$i = 0;
			// This will eventually have to call out to see how much information
			// the user is allowed to know about the exit
			foreach($room->exits as $exit) {
				$tmp += array($i=>clientHandler::serialize_visible_exit($exit));
				$i++;
			}
		}
		
		return array(VISIBLE_EXITS=>$tmp);
	}
	
	public static function serialize_people($user) {
		$obj=$user->in_room();
		$tmp = array();
		
		// The people in the room
		if (isset($obj->people)) {
			$i = 0;
			// This will eventually have to call out to see how much information
			// the user is allowed to know about the given person
			foreach($obj->people as $person) {
				$tmp += array($i=>clientHandler::serialize_visible_person($person));
				$i++;
			}
		}
			
		return array(PEOPLE_IN_ROOM=>$tmp);
	}
	
	public static function serialize_items($user) {
		$room=$user->in_room();
		$tmp = array();
		
		// The people in the room
		if (isset($room->items)) {
			$i = 0;
			// This will eventually have to call out to see how much information
			// the user is allowed to know about the given person
			foreach($room->items as $item) {
				$tmp += array($i=>clientHandler::serialize_visible_item($person));
				$i++;
			}
		}
			
		return array(ITEMS_IN_ROOM=>$tmp);
	}
	
	
	public static function serialize_in_room($user) {
		$room=$user->in_room();
		$tmp = array();
		$tmp += array(IN_ROOM=>clientHandler::serializeobject($room)); // Easy.
		$tmp += clientHandler::serialize_people($user);
		$tmp += clientHandler::serialize_visible_exits($user);
		$tmp += clientHandler::serialize_items($user);
		
		return $tmp;
	}
	
	public static function serialize_exit($exit) {
		$exitArray = clientHandler::serializeobject($exit);
		$exitArray['command'] = $exit->command;
		
		// Now to add the rooms it links to
		$i = 0;
		foreach(World::$room_list as $room) {
			if($room === $exit->to_room) {
				$exitArray['to_room'] = $i;
				break;
			}
			
			$i++;
		}
		
		return $exitArray;
	}
	
	public static function serialize_room_list($user) {
		$roomListArray = array();		
		$roomListArray['roomlist'] = array();
		
		$i = 0;
		
		foreach(World::$room_list as $room) {
			// Get the room data
			$roomListArray['roomlist'][$i]['room'] = clientHandler::serializeobject($room);
			
			// TODO: Get the exit data for the room
			$exit_id = 0;
			foreach($room->exits as $exit) {
				$roomListArray['roomlist'][$i]['exit'][$exit_id] = clientHandler::serialize_exit($exit);
			
				$exit_id++;
			}
			
			$i++;
		}
		
		return $roomListArray;
	}
}

?>